import { myFetch } from "../utils/fetch"
import { defineSource } from '../source';
import { ProcessedItem } from '../types'
import logger from '../../framework/config/logger';

interface Res {
    data: {
        cards: {
            content: {
                isTop?: boolean;
                word: string;
                hotScore: string;
                rawUrl: string;
                desc?: string;
            }[];
        }[];
    };
}

export default defineSource({
    baidu: async (): Promise<ProcessedItem[]> => {
        const rawData = await myFetch(`https://top.baidu.com/board?tab=realtime`);
        const jsonStr = rawData.match(/<!--s-data:(.*?)-->/s);
    
        if (!jsonStr || !jsonStr[1]) {
            throw new Error("Failed to extract JSON from response");
        }
    
        let data: Res;
        try {
            data = JSON.parse(jsonStr[1]);
        } catch (e) {
            throw new Error("Invalid JSON data in response");
        }
    
        return data.data.cards[0].content
            .filter(k => !k.isTop)
            .map((k): ProcessedItem => ({
                id: k.rawUrl,
                title: k.word,
                url: k.rawUrl,
                extra: {
                    hot: k.hotScore,
                    content: k.desc,
                },
            }));
    }
});